var geocoder;
var map;
var markers = Array();
var infos = Array();
var icon_url;
var main_icon_url;
var service;
var ser;
var radious;
var infowindow;
var pag;
var t;
var time;
function initialize() {
    // prepare Geocoder
    geocoder = new google.maps.Geocoder();
t=2;

    // set initial position (New York)
  
        var myLatlng = new google.maps.LatLng(40.7143528,-74.0059731);
main_icon_url=document.getElementById("mainPin").value;

    var myOptions = { // default map options
        scrollwheel: false,
        zoom: 15,
        center: myLatlng,
        mapTypeId: google.maps.MapTypeId.ROADMAP
    }
	
    map = new google.maps.Map(document.getElementById('gmap_canvas'), myOptions);
        
  var address = document.getElementById("address").value;

    // script uses our 'geocoder' in order to find location by address name
    geocoder.geocode( { 'address': address}, function(results, status) {
        if (status == google.maps.GeocoderStatus.OK) { // and, if everything is ok

            // we will center map
            var addrLocation = results[0].geometry.location;
            map.setCenter(addrLocation);

            // store current coordinates into hidden variables
            
            document.getElementById('lng').value = results[0].geometry.location.lng();
        document.getElementById('lat').value = results[0].geometry.location.lat();      
        

            // and then - add new custom marker
            var addrMarker = new google.maps.Marker({
                position: addrLocation,
                map: map,
                title: results[0].formatted_address,
             icon: main_icon_url
            });
        } else {
            alert('Geocode was not successful for the following reason: ' + status);
                        
        }
    });
        service = new google.maps.places.PlacesService(map);
		ser=new google.maps.places.PlacesService(map);
}

// clear overlays function

function clearOverlays() {
  if(markers){
       for( var i = 0; i < markers.length; i++ ){
            markers[i].setMap(null);
        }
        markers = [];
        infos = [];
}
}

// clear infos function
function clearInfos() {
    if (infos) {
        for( var i = 0; i < infos.length; i++ )  {
            if (infos[i].getMap()) {
                infos[i].close();
            }
        }
    }
}


// find address functio
 
// find custom places function
function findPlaces(place,url) {

 radious=1000;

    // prepare variables (filter)
icon_url=url;

    var lat = document.getElementById('lat').value;
    var lng = document.getElementById('lng').value;
    var cur_location = new google.maps.LatLng(lat, lng);

    // prepare request to Places
    var request = {
        location: cur_location,
        radius: radious,
        types: [place],                
    };


    // send request

t=t+0.16;
time=time*t;
    setTimeout(function() {
                createMarker( service.search(request, createMarkers));
            }, 4000);

}

// create markers (from 'findPlaces' function)
function createMarkers(results, status,pagination) {
   

   if (status == google.maps.places.PlacesServiceStatus.OK) {

        // if we have found something - clear map (overlays)

        clearOverlays();

        // and create new markers by search result
        for (var i = 0, num = results.length; i<num; i++) {
		setTimeout(createMarker( results[i] ,i) , i*i*1500);
           // createMarker(results[i],pagination);
			
        }
    } else if (status == google.maps.places.PlacesServiceStatus.ZERO_RESULTS) {
        alert('Sorry, nothing is found');
    }

}

// creare single marker function



function createMarker(place, i) {
    // prepare new Marker object
    var mark = new google.maps.Marker({

        position: place.geometry.location,

        map: map,

        title: place.name,
    icon:icon_url

    });
markers.push(mark);
 
 infowindow = new google.maps.InfoWindow();

 var req = {
    reference: place.reference 
  
  };

    service.getDetails(req, function(place, status){
	if (status === google.maps.places.PlacesServiceStatus.OK){
   
 google.maps.event.addListener(mark, 'click', function() {
 
    infowindow.setContent('<b>'+place.name+'</b><br>'+place.formatted_phone_number+'<br>'+place.formatted_address);
    infowindow.open(map,mark);

	});
	}
	else if (status === google.maps.GeocoderStatus.OVER_QUERY_LIMIT) {
	
	setTimeout(function() {
		
	
	 ser.getDetails(req, function(place, status){
	 if (status === google.maps.places.PlacesServiceStatus.OK){
	 //alert(place.formatted_phone_number);
              google.maps.event.addListener(mark, 'click', function() {
 
    infowindow.setContent('<b>'+place.name+'</b><br>'+place.formatted_phone_number+'<br>'+place.formatted_address);
    infowindow.open(map,mark);

	});}});
             }, i*250);
			}
});

google.maps.event.addListener(mark, 'click', function() {
 
    infowindow.setContent('<b>'+place.name+'</b><br>'+'Updating...'+'<br>'+place.vicinity );
    infowindow.open(map,mark);

	});

	infos.push(infowindow);



}


    // add event handler to current marker
  
   
    

// initialization
google.maps.event.addDomListener(window, 'load', initialize);
//google.maps.event.addListener(map, 'mouseout', function(event){
 //this.setOptions({scrollwheel:false});  
//});


$(document).ready(function(e) {
    $('#list-menu a').click(function(){
    $(this).addClass('active');
    $('#list-menu a').not(this).removeClass('active');
    });
});

